﻿Imports System.IO
Imports System.Xml.Serialization

Public Class Gerais

    Public Shared Function ConverterSeqBytesEmString(ByVal _SequenciaBytes As String) As String
        Dim ArrAux() As String
        Dim i As Long
        Dim Str As String = ""
        Try
            If _SequenciaBytes Is Nothing Then Return Nothing
            ArrAux = _SequenciaBytes.Split("-")
            For i = 0 To ArrAux.Length - 1
                If IsNumeric(ArrAux(i)) Then
                    Str &= Chr(ArrAux(i))
                Else
                    Return _SequenciaBytes
                End If
            Next
            Return Str

        Catch ex As Exception
            Return _SequenciaBytes

        End Try
    End Function

    Public Shared Function ConverterStringEmSeqBytes(ByVal _String As String) As String
        Try
            Dim sSeqByt As String = ""
            Dim i As Long
            For i = 0 To _String.Length - 1
                sSeqByt &= CType(Asc(_String.Chars(i)), String) & IIf(i = (_String.Length - 1), "", "-")
            Next
            Return sSeqByt
        Catch ex As Exception
            Throw New Exception(ex.Message, ex)
        End Try
    End Function

    Public Shared Function MontaXml(Of T)(ByVal Objeto As T) As String

        Dim xml As String = Serialize(Of T)(Objeto)

        Return RetiraEnconding(xml)

    End Function

    Public Shared Function MontaXml(Of T)(ByVal Lista As List(Of T), ByVal TipoObjeto As String) As String

        Dim xml As String = Serialize(Of List(Of T))(Lista)

        xml = xml.Replace("ArrayOf" & TipoObjeto, "XML")

        Return RetiraEnconding(xml)

    End Function

    Private Shared Function RetiraEnconding(ByVal Xml As String) As String

        Return Xml.Replace("<?xml version=""1.0"" encoding=""iso-8859-1""?>", "")

    End Function

    Public Shared Function Serialize(Of T)(ByVal Objeto As T) As String

        Dim encoding As System.Text.Encoding = System.Text.Encoding.GetEncoding("iso-8859-1")

        Dim stream As New MemoryStream()

        Dim writer As New XmlSerializer(GetType(T))

        Dim textWriter As New System.Xml.XmlTextWriter(stream, encoding)

        Dim namespaces As New XmlSerializerNamespaces()

        namespaces.Add(String.Empty, String.Empty)

        writer.Serialize(textWriter, Objeto, namespaces)

        stream = textWriter.BaseStream

        Dim xml As String = encoding.GetString(stream.ToArray())

        Return xml

    End Function

    Public Shared Function Deserialize(Of T)(ByVal Xml As String) As T

        If Not Xml.StartsWith("<?", StringComparison.OrdinalIgnoreCase) Then

            Xml = "<?xml version=""1.0"" encoding=""iso-8859-1""?>" & Xml

        End If

        Dim xmlReader As New XmlSerializer(GetType(T))

        Dim encoding As System.Text.Encoding = System.Text.Encoding.GetEncoding("iso-8859-1")

        Dim stream As New System.IO.MemoryStream(encoding.GetBytes(Xml))

        Dim objeto As T = CType(xmlReader.Deserialize(stream), T)

        Return objeto

    End Function

    Public Shared Function ObjectEquals(Of T)(ByVal Objeto1 As T, ByVal Objeto2 As T) As Boolean

        If (IsNothing(Objeto1) AndAlso IsNothing(Objeto2)) OrElse (Not IsNothing(Objeto1) AndAlso Not IsNothing(Objeto2) AndAlso Objeto1.Equals(Objeto2)) Then

            Return True

        End If

        Return False

    End Function

    Public Shared Function ListEquals(Of T)(ByVal Lista1 As List(Of T), ByVal Lista2 As List(Of T)) As Boolean

        If Lista1.Count <> Lista2.Count Then

            Return False

        End If

        For Each item1 As T In Lista1

            Dim qtdItens2Diff As Integer = 0

            For Each item2 As T In Lista2

                If Not item1.Equals(item2) Then

                    qtdItens2Diff += 1

                End If

            Next

            If qtdItens2Diff = Lista2.Count Then

                Return False

            End If

        Next

        Return True

    End Function

    Public Shared Function CompararListas(Of T)(ByVal lista1 As List(Of T), ByVal lista2 As List(Of T)) As Boolean

        Try

            If lista1.Count <> lista2.Count Then
                Return False
            End If

            For Each obj As T In lista1

                Dim valorEncontrado As Boolean = False

                For Each objLista2 As T In lista2


                    Dim EqualEspecifico = GetType(T).GetMethod("Equals", New Type() {GetType(T)})

                    If Not IsNothing(EqualEspecifico) AndAlso EqualEspecifico.ReturnType.Equals(GetType(Boolean)) Then

                        If EqualEspecifico.Invoke(obj, New Object() {objLista2}) Then
                            valorEncontrado = True
                            Exit For
                        End If

                    Else

                        If obj.Equals(objLista2) Then

                            valorEncontrado = True
                            Exit For

                        End If

                    End If

                Next

                If valorEncontrado = True Then

                    Continue For

                Else

                    Return False

                End If

            Next

            Return True

        Catch ex As Exception

            Throw New Exception(ex.Message, ex)

        End Try

    End Function

End Class
